//
//  MainTabBarController.m
//  梦香菜谱
//
//  Created by imac on 15/9/28.
//  Copyright (c) 2015年 imac. All rights reserved.
//

#import "ByTabBarController.h"

#import "AppDelegate.h"

// 子控制器
#import "ByMyVC.h"
#import "ByNewsVC.h"
#import "ByShopVC.h"
#import "ByHomeVC.h"

@interface ByTabBarController ()
{
    NSInteger _curIndex;
}
@end

@implementation ByTabBarController


+ (ByTabBarController *)shareSigleton{
    UIApplication *appliction = [UIApplication sharedApplication];
    AppDelegate *delegate =(AppDelegate*) appliction.delegate;
    ByTabBarController * tabBarVC = (ByTabBarController*)delegate.window.rootViewController ;
    return tabBarVC;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    // 隐藏系统的TabBar
    [self.tabBar removeFromSuperview];
    
    // 创建定自定义的TabBar
    [self _createTabBar];
   
    // 创建子控制器
    [self _createViewControllers];
}

// 创建自定义的TabBar
- (void)_createTabBar {
    
    // 未选中状态图片
    NSArray *imageNames = @[
                            @"tabbar_shouye",
                            @"tabbar_shangcheng",
                            @"tabbar_xiaoxi",
                            @"tabbar_geren",
                            ];
    // 选中状态图片
    NSArray *selectedImageNames = @[
                                    @"tabbar_shouye_selected",
                                    @"tabbar_shangcheng_selected",
                                    @"tabbar_xiaoxi_selected",
                                    @"tabbar_geren_selected",
                                    ];
    // 标签栏标题
    NSArray *titles = @[
                        @"首页",
                        @"商城",
                        @"消息",
                        @"我的",
                        ];
    
    // 创建整个TabBar
    _tabBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49, SCREEN_WIDTH, 49)];
    self.tabBarView.backgroundColor = [UIColor whiteColor];
    _tabBarView.userInteractionEnabled = YES;
    _tabBarView.contentMode = UIViewContentModeScaleToFill;
    // 添加分隔线
    UIView *separateLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    separateLine.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [_tabBarView addSubview:separateLine];
    [self.view addSubview:_tabBarView];
    
    // 创建tabBar按钮
    CGFloat buttonWidth = SCREEN_WIDTH / titles.count;
    for (int i = 0; i < titles.count; i++) {
        // 未选中图片
        UIImage *buttonImage = [UIImage imageNamed:imageNames[i]];
        // 选中图片
        UIImage *selectedImage = [UIImage imageNamed:selectedImageNames[i]];
        ByTabBarButton *button = [[ByTabBarButton alloc] initWithFrame:CGRectMake(i * buttonWidth, 0, buttonWidth, 49) Image:buttonImage SelectedImage:selectedImage Title:titles[i]];
        button.tag = i + 10;
        if (i == 0) {
            // 刚开始设置第一个按钮为选中按钮
            button.isSelectedButton = YES;
            self.selectedButton = button;
        }
        [button addTarget:self action:@selector(_buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBarView addSubview:button];
    }
}

// tabBar按钮点击事件
- (void)_buttonAction:(ByTabBarButton *)button{
    
    /***判断是否登录*/
//    if (button.tag != 10 && button.tag != 14) {
//        ByNetUserModel *user = [ByCommonCache getUserInfo];
//        if (!user.uid) {
//            ByLoginVC *loginVC = [[ByLoginVC alloc] init];
//            ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:loginVC];
//            [self presentViewController:navi animated:YES completion:nil];
//            return;
//        }
//    }
    
    self.selectedIndex = button.tag -10;
    
//    if (self.selectedIndex == 0) {
//        ByBaseNaviController *nav = [self.viewControllers firstObject];
//        for (ByBaseViewController *vc in nav.viewControllers) {
//            if (![vc isKindOfClass:[ByHomeViewController class]]) {
//                [nav popToRootViewControllerAnimated:YES];
//            }
//        }
//        
//    }
//    
//    if (self.selectedIndex == 2){
//        
//        ByBaseNaviController *nav = self.viewControllers[2];
//        for (ByBaseViewController *vc in nav.viewControllers) {
//            if (![vc isKindOfClass:[ByCartViewController class]]) {
//                [nav popToRootViewControllerAnimated:YES];
//            }
//        }
//        
//    }
    
    
    
    // 上一次选中的按钮状态改为no
    self.selectedButton.isSelectedButton = NO;
    // 当前被点击的按钮状态改为yes
    button.isSelectedButton = YES;
    // 记录当前选中按钮
    self.selectedButton = button;
    
    
    
}



- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    
    ByTabBarButton *button = [_tabBarView viewWithTag:(NSInteger)selectedIndex + 10];
    if ([button isKindOfClass:[UIButton class]]) {
        self.selectedButton.isSelectedButton = NO;
        button.isSelectedButton = YES;
        self.selectedButton = button;
    }
    
    if (selectedIndex == 0) {
        ByBaseNaviController *nav = [self.viewControllers firstObject];
        for (ByBaseVC *vc in nav.viewControllers) {
            if (![vc isKindOfClass:[ByHomeVC class]]) {
                [nav popToRootViewControllerAnimated:YES];
            }
        }
        
    }
    
}


#pragma mark -- 创建控制器 --
- (void)_createViewControllers {
    // 屋顶（首页）
    ByHomeVC *vc1 = [[ByHomeVC alloc] init];
    ByBaseNaviController *navi1 = [[ByBaseNaviController alloc] initWithRootViewController:vc1];

    ByShopVC *vc2 = [[ByShopVC alloc] init];
    ByBaseNaviController *navi2 = [[ByBaseNaviController alloc] initWithRootViewController:vc2];
    
    ByNewsVC *vc3 = [[ByNewsVC alloc] init];
    ByBaseNaviController *navi3 = [[ByBaseNaviController alloc] initWithRootViewController:vc3];
    
    ByMyVC *vc4 = [[ByMyVC alloc] init];
    ByBaseNaviController *navi4 = [[ByBaseNaviController alloc] initWithRootViewController:vc4];
    self.viewControllers = @[navi1,navi2,navi3,navi4];
}


#pragma mark - 标签栏隐藏和显示
// 隐藏标签栏
- (void)hideTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        _tabBarView.top = SCREEN_HEIGHT;
    }];
}


// 标签栏出现
- (void)showTabBar
{
    [UIView animateWithDuration:0.3 animations:^{
        _tabBarView.bottom = SCREEN_HEIGHT;
    }];
}
@end
